package forms;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import actions.ActionManager;

public class MainForm extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JMenu menu;
	JMenuBar menuBar;
	JMenuItem menuItem1;
	JMenuItem quitItem;
	JMenuItem optionsItem;
	ActionManager actionManager;
	private static MainForm instance = null;
	public MainForm() {
		actionManager = new ActionManager();
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		setLayout(new BorderLayout());
		setTitle("UberPoslovna Aplikacija");
		optionsItem = new JMenuItem("Options");
		optionsItem.addActionListener(actionManager.getOpenOptionsFormAction());
		menuItem1 = new JMenuItem("Drzave");
		menuItem1.addActionListener(actionManager.getOpenDrzaveFormAction());
		quitItem = new JMenuItem("Quit");
		quitItem.addActionListener(actionManager.getQuitAction());
		menu = new JMenu("Menu");
		menu.add(menuItem1);
		menu.addSeparator();
		menu.add(optionsItem);
		menu.addSeparator();
		menu.add(quitItem);
		menuBar = new JMenuBar();
		menuBar.add(menu);
		add(menuBar,BorderLayout.NORTH);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(actionManager.getQuitAction());
	}
	
	public static void main(String args[]) {
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension screenSize = toolkit.getScreenSize();
		MainForm mf = MainForm.getInstance();
		mf.pack();	
		mf.setSize((int)(screenSize.getWidth()*0.666f), (int)(screenSize.getHeight()*0.666f));
		//mf.setLocation((int)(screenSize.getWidth()/2-400), (int)(screenSize.getHeight()/2-300));
		mf.setLocationRelativeTo(null);		
		mf.setVisible(true);
	}

	public static MainForm getInstance() {
		if(instance==null) {
			instance = new MainForm();
		}
		return instance;
	}
	
	public void setLookAndFeel(String lfName) {
		Dimension windowSize = MainForm.getInstance().getSize();
		try {
			UIManager.setLookAndFeel(lfName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		SwingUtilities.updateComponentTreeUI(MainForm.getInstance());
		MainForm.getInstance().pack();
		MainForm.getInstance().setSize(windowSize);
	}
	
}
